<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MainController extends CI_Controller {



	public function index()
	{
		$this->load->view('home/index.php');
		
	}
	
	private function sessStart($idcompany,$idqr)
	{

		$sessData = array(
				'idcompany'  => $idcompany,
				'idqr'     => $idqr,
				'logged_in' => TRUE,
		);	
		$this->session->set_userdata($sessData);
		if(!$this->session->userdata('my_session_id'))
		{
			
			$this->session->set_userdata('my_session_id',$this->session->userdata('session_id'));
		}
		
		
		
	}
	
	private function Check($idcompany,$idqr)
	{

		if(isset($idcompany) && isset($idqr))
		{

			$this->load->database();
			$sql = "SELECT * FROM qr_code WHERE idcompany = ? AND id = ?";
			$query = $this->db->query($sql,array($idcompany,$idqr));
			
			$row=$query->row_array();
			
			if(isset($row['id']))
			{
				return true;
			}
			else	
			{
				echo "No concuerda el Hotel con el Codigo";
				$this->session->unset_userdata('metfindData');
				return false;
			}
		}
		else
		{
			echo "Url mal escrita";	
			$this->session->unset_userdata('metfindData');

			return false;
		}
	}
	public function sessQE()
	{

		$sessData=$this->input->post('QE');
		$this->session->unset_userdata('questionev');
		$this->session->set_userdata('questionev',$sessData);
		print_r($this->session->userdata('questionev'));
	
	}
	public function sessEE()
	{
		$sessData=$this->input->post('EE');
		
		
		$this->session->unset_userdata('employeeev');
		$this->session->set_userdata('employeeev',$sessData);
		print_r($this->session->userdata('employeeev'));
	}
	public function sessCO()
	{
		$sessData=$this->input->post('CO');
		
		
		$this->session->unset_userdata('commentev');
		$this->session->set_userdata('commentev',"comment:".$sessData);
		print_r($this->session->userdata('commentev'));
	}
	

	public function Hotel($idcompany,$idqr)
	{
		if($this->Check($idcompany,$idqr))
		{
			$this->sessStart($idcompany,$idqr);
			
			$this->load->view('mainview.php');
		}
	}
	
	
	public function AJAX_Header()
	{
		$idcompany=$this->session->userdata('idcompany');
		$idqr=$this->session->userdata('idqr');
		
		if($this->Check($idcompany,$idqr))
		{
			$this->load->database();
			$sql = "SELECT logo FROM company WHERE id = ?";
			$query = $this->db->query($sql,array($idcompany));
			$data['company']=$query->row_array();
			
			$this->load->view('ajax/header.php',$data);
		}
	}
	
	public function AJAX_Employee()
	{	
		$idcompany=$this->session->userdata('idcompany');
		$idqr=$this->session->userdata('idqr');
		
		if($this->Check($idcompany,$idqr))
		{
			$this->load->database();
			$sql = "SELECT name,id,photo FROM employee WHERE idcompany = ?";
			$query = $this->db->query($sql,array($idcompany));
			$data['employee']=$query->result_array();
			
			$this->load->view('ajax/employee.php',$data);
		}
	}
	public function AJAX_Home()
	{	
		$idcompany=$this->session->userdata('idcompany');
		$idqr=$this->session->userdata('idqr');
		
		
		if($this->Check($idcompany,$idqr))
		{	
			$this->load->database();
			$sql = "SELECT question,id FROM question WHERE idcompany = ?";
			$query = $this->db->query($sql,array($idcompany));
			$data['question']=$query->result_array();
			
			$sql = "SELECT name,logo FROM company WHERE id = ?";
			$query = $this->db->query($sql,array($idcompany));
			$data['company']=$query->row_array();		
			
			$sql = "SELECT place  FROM qr_code WHERE id = ?";
			$query = $this->db->query($sql,array($idqr));
			$data['qr_code']=$query->row_array();
	
			$this->load->view('ajax/home.php',$data);
		}

	}
	public function AJAX_Write()
	{
		$this->load->view('ajax/write.php');
	}
	public function AJAX_Send()
	{
		$this->load->view('ajax/send.php');
	}
	public function AJAX_Footer()
	{
		$this->load->view('ajax/footer.php');
	}
	
	public function AJAX_Info()
	{
		$this->load->view('ajax/info.php');
	}
	
	public function saveData()
	{
		$idcompany=$this->session->userdata('idcompany');
		$idqr=$this->session->userdata('idqr');
		
		
		if($this->Check($idcompany,$idqr))
		{
			
			$session_id=$this->session->userdata('session_id');
			$user_agent=$this->session->userdata('user_agent');
			$ip_address=$this->session->userdata('ip_address');
			$last_activity=$this->session->userdata('last_activity');
			$my_session_id=$this->session->userdata('my_session_id');
			$employeeev=$this->session->userdata('employeeev');
			$questionev=$this->session->userdata('questionev');
			$comment=substr($this->session->userdata('commentev'),8);
			$name=$this->input->post('name',TRUE);
			$mail=$this->input->post('mail',TRUE);
			$phone=$this->input->post('phone',TRUE);
			
			$sql = "SELECT iduser FROM web_user WHERE session_id = ?";
			$query = $this->db->query($sql,array($my_session_id));
			$iduser=$query->row_array();
			
			if(isset($iduser['iduser']))
			{
				$iduser=$iduser['iduser'];
				//UPDATE USER
				
				if($name!='')
				{
					$sql = "UPDATE user SET name=? WHERE id=?";
					$query = $this->db->query($sql,array($name,$iduser));	
				}
				if($mail!='')
				{
					$sql = "UPDATE user SET email=? WHERE id=?";
					$query = $this->db->query($sql,array($mail,$iduser));
				}
				if($phone)
				{
					$sql = "UPDATE user SET phone=? WHERE id=?";
					$query = $this->db->query($sql,array($phone,$iduser));
				}
				$sql = "UPDATE user SET last_activity=? WHERE id=?";
				$query = $this->db->query($sql,array(date("Y-m-d H:i:s", time()),$iduser));
				echo $last_activity;
				//
				$sql = "INSERT into evaluation (idqr_code,comment) values (?,?)";
				$query = $this->db->query($sql,array($idqr,$comment));
				$idevaluation=$this->db->insert_id();
				//
				$sql = "INSERT into web_user (iduser,idevaluation,session_id,user_agent,ip_address) values (?,?,?,?,?)";
				$query = $this->db->query($sql,array($iduser,$idevaluation,$my_session_id,$user_agent,$ip_address));
				//
				
				foreach($questionev as $qev)
				{
				
					$sql = "INSERT into question_evaluation (idquestion,idevaluation,rating) values (?,?,?)";
					$query = $this->db->query($sql,array($qev[0],$idevaluation,$qev[1]));
				}
				foreach($employeeev as $eev)
				{
						
					$sql = "INSERT into employee_evaluation (idemployee,idevaluation,rating,comment) values (?,?,?,?)";
					$query = $this->db->query($sql,array($eev[0],$idevaluation,$eev[1],$eev[2]));
				}
			}
			else
			{
				//create user
				$sql = "INSERT into user (name,email,phone) values (?,?,?)";
				$query = $this->db->query($sql,array($name,$mail,$phone));
				$iduser = $this->db->insert_id();
				
				//
				$sql = "INSERT into evaluation (idqr_code,comment) values (?,?)";
				$query = $this->db->query($sql,array($idqr,$comment));
				$idevaluation=$this->db->insert_id();
				//
				$sql = "INSERT into web_user (iduser,idevaluation,session_id,user_agent,ip_address) values (?,?,?,?,?)";
				$query = $this->db->query($sql,array($iduser,$idevaluation,$my_session_id,$user_agent,$ip_address));
				//
				
				foreach($questionev as $qev)
				{

					$sql = "INSERT into question_evaluation (idquestion,idevaluation,rating) values (?,?,?)";
					$query = $this->db->query($sql,array($qev[0],$idevaluation,$qev[1]));		
				}
				foreach($employeeev as $eev)
				{
					
					$sql = "INSERT into employee_evaluation (idemployee,idevaluation,rating,comment) values (?,?,?,?)";
					$query = $this->db->query($sql,array($eev[0],$idevaluation,$eev[1],$eev[2]));
				}
			}
			
			$this->session->unset_userdata('questionev');
			$this->session->unset_userdata('employeeev');
			$this->session->unset_userdata('commentev');
		}
		
	}
}